const mongoose = require('mongoose');
const bcryptjs = require('bcryptjs')
// 这里的流程官网上有，讲的很清楚，每一步是干什么的
const Schema = mongoose.Schema;
const userSchema = new Schema({
    username: { type: String },
    password: {
        type: String,
        // 查询数据时默认不把password字段带出来
        select: false,
        // 密码加密
        set(val) {
            return bcryptjs.hashSync(val, 10)
        }
    },
    avatar: {
        type: String,
        default: "/uploads/avatar.png"
    },
    gender: {
        type: Number,
        default: 1
    },
    birthday: {
        type: Date,
        default: new Date(0)
    },
    email: {
        type: String,
        default: ""
    },
    createDate: {
        type: Date,
        default: Date.now()
    },
    group: {
        type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'group'}]
    }
}, {
    collection: 'user', // 这里是为了避免新建的表会带上 s 后缀
    versionKey: false // 不需要 __v 字段，默认是加上的
});

module.exports = mongoose.model('user', userSchema);